<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>操作系统学习笔记-单处理器调度 | 花猪のBlog</title><meta name="keywords" content="操作系统"><meta name="author" content="花猪,2296412185@qq.com"><meta name="copyright" content="花猪"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="记录操作系统学习历程">
<meta property="og:type" content="article">
<meta property="og:title" content="操作系统学习笔记-单处理器调度">
<meta property="og:url" content="https://cnhuazhu.top/2021/05/30/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%8D%95%E5%A4%84%E7%90%86%E5%99%A8%E8%B0%83%E5%BA%A6/index.html">
<meta property="og:site_name" content="花猪のBlog">
<meta property="og:description" content="记录操作系统学习历程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png">
<meta property="article:published_time" content="2021-05-30T06:17:26.000Z">
<meta property="article:modified_time" content="2021-06-24T08:09:14.144Z">
<meta property="article:author" content="花猪">
<meta property="article:tag" content="操作系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png"><link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/avatar.jpg"><link rel="canonical" href="https://cnhuazhu.top/2021/05/30/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%8D%95%E5%A4%84%E7%90%86%E5%99%A8%E8%B0%83%E5%BA%A6/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":60,"languages":{"author":"作者: 花猪","link":"链接: ","source":"来源: 花猪のBlog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#ABADAC","bgDark":"#121212","position":"bottom-right"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '操作系统学习笔记-单处理器调度',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-24 16:09:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><link rel="stylesheet" href="/css/tag.css"><link rel="stylesheet" href="/css/pagelucency.css"><style>#article-container.post-content h1:before, h2:before, h3:before, h4:before, h5:before, h6:before { -webkit-animation: avatar_turn_around 1s linear infinite; -moz-animation: avatar_turn_around 1s linear infinite; -o-animation: avatar_turn_around 1s linear infinite; -ms-animation: avatar_turn_around 1s linear infinite; animation: avatar_turn_around 1s linear infinite; }</style><link rel="stylesheet" href="/css/avatarbg.css"><link rel="stylesheet" href="/css/footerlucency.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/macblack.css"><link rel="stylesheet" href="/css/mouse.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-swiper/lib/swiper.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-swiper/lib/swiperstyle.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="花猪のBlog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="wizard-scene"><div class="wizard-objects"><div class="wizard-square"></div><div class="wizard-circle"></div><div class="wizard-triangle"></div></div><div class="wizard"><div class="wizard-body"></div><div class="wizard-right-arm"><div class="wizard-right-hand"></div></div><div class="wizard-left-arm"><div class="wizard-left-hand"></div></div><div class="wizard-head"><div class="wizard-beard"></div><div class="wizard-face"><div class="wizard-adds"></div></div><div class="wizard-hat"><div class="wizard-hat-of-the-hat"></div><div class="wizard-four-point-star --first"></div><div class="wizard-four-point-star --second"></div><div class="wizard-four-point-star --third"></div></div></div></div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">86</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="http://cnhuazhu.top/"><i class="fa-fw fas fa-gift"></i><span> 引导</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-sitemap"></i><span> 分站</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="http://cnhuazhu.top/next"><i class="fa-fw fas fa-chevron-right"></i><span> Next</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 图库</span></a></li><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book"></i><span> 阅读</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-film"></i><span> 电影</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fa fa-video"></i><span> 视频</span></a></li><li><a class="site-page child" href="/artitalk/"><i class="fa-fw fas fa-quote-left"></i><span> 说说</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></li><li><a class="site-page child" href="/website/"><i class="fa-fw fas fa-globe"></i><span> 网站</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/Message/"><i class="fa-fw fas fa-comment"></i><span> 留言</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background: transparent"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">花猪のBlog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="http://cnhuazhu.top/"><i class="fa-fw fas fa-gift"></i><span> 引导</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-sitemap"></i><span> 分站</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="http://cnhuazhu.top/next"><i class="fa-fw fas fa-chevron-right"></i><span> Next</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 图库</span></a></li><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book"></i><span> 阅读</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-film"></i><span> 电影</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fa fa-video"></i><span> 视频</span></a></li><li><a class="site-page child" href="/artitalk/"><i class="fa-fw fas fa-quote-left"></i><span> 说说</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></li><li><a class="site-page child" href="/website/"><i class="fa-fw fas fa-globe"></i><span> 网站</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/Message/"><i class="fa-fw fas fa-comment"></i><span> 留言</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">操作系统学习笔记-单处理器调度</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-05-30T06:17:26.000Z" title="发表于 2021-05-30 14:17:26">2021-05-30</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-24T08:09:14.144Z" title="更新于 2021-06-24 16:09:14">2021-06-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>15分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="操作系统学习笔记-单处理器调度"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1>前言</h1>
<p><em>正在学习操作系统，记录笔记。</em></p>
<blockquote>
<p>参考资料：</p>
<p>《操作系统（精髓与设计原理 第8版） 》</p>
</blockquote>
<hr>
<h1>第九章：单处理器调度</h1>
<p>多道程序设计的关键就是调度。典型的调度有4种（见下表）</p>
<table>
<thead>
<tr>
<th>调度的类型</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>长程调度（Long-term scheduling）</td>
<td>决定加入待执行进程池</td>
</tr>
<tr>
<td>中程调度（Medium-term scheduling）</td>
<td>决定加入部分或全部位于内存中的进程集合</td>
</tr>
<tr>
<td>短程调度（Short-term scheduling）</td>
<td>决定处理器执行哪个可运行进程</td>
</tr>
<tr>
<td>I/O调度（I/O scheduling）</td>
<td>决定可用I/O设备处理哪个进程挂起的I/O请求</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本章先介绍前三种，I/O调度将在后续章节中介绍。</p>
</blockquote>
<h2 id="处理器调度的类型">处理器调度的类型</h2>
<p>首先要明白调度的目的：根本目的就是提升CPU效率。</p>
<p>有以下三个指标：</p>
<ul>
<li>响应时间（Response time ）</li>
<li>吞吐率（Throughput）</li>
<li>处理器效率（Processor efficiency ）</li>
</ul>
<p>结合进程状态图具体展示出不同调度类型的功能：</p>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E8%BF%9B%E7%A8%8B%E7%8A%B6%E6%80%81%E5%9B%BE%E7%BB%93%E5%90%88%E8%B0%83%E5%BA%A6%E7%B1%BB%E5%9E%8B.png" alt=""></p>
<blockquote>
<p>说明：</p>
<ul>
<li>长程调度：在进程创建的时候执行，它决定是否把进程添加到当前活跃的进程集中。
<ul>
<li>新建态 → 就绪/挂起态</li>
<li>新建态 → 就绪态</li>
</ul>
</li>
<li>中程调度：是交换功能的一部分，它决定是否把进程添加到那些至少部分已在内存且可被执行的进程集中。
<ul>
<li>就绪/挂起态 → 就绪态</li>
<li>阻塞/挂起态 → 阻塞态</li>
</ul>
</li>
<li>短程调度：真正决定下次执行哪个就绪进程。
<ul>
<li>就绪态 → 运行态</li>
</ul>
</li>
<li>（具体长、中、短的含义指的是：进程完成调度的时间）</li>
</ul>
<hr>
<p>下图重新组织了进程状态转换图：</p>
<img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS进程状态转换图新.png" style="zoom:70%;" />
<blockquote>
<p>值得注意的是：</p>
<ul>
<li>长程调度一定嵌套短程调度，但不一定嵌套中程调度。</li>
<li>中程调度一定嵌套短程调度。</li>
</ul>
</blockquote>
<hr>
<p>下图给出了进程转换过程中所涉及的队列：</p>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E8%B0%83%E5%BA%A6%E6%B6%89%E5%8F%8A%E7%9A%84%E9%98%9F%E5%88%97.png" alt=""></p>
<blockquote>
<p>调度的本质：就是对各种队列的管理，以提高系统的利用率。</p>
</blockquote>
</blockquote>
<h3 id="长程调度">长程调度</h3>
<p>长程调度程序决定了哪个程序可以进入系统中进行处理，因此它<strong>控制了系统的并发度</strong>。</p>
<blockquote>
<p>长程调度主要出现在批处理系统中，又叫做：作业调度。</p>
</blockquote>
<p>长程调度程序运行时，从队列中创建相应的进程，这时会涉及两个决策：</p>
<ul>
<li>决定操作系统何时才能接纳一个或多个进程
<ul>
<li>创建的进程越多，每个进程占用处理器的时间就越少（因此长程调度程序会限制并发度）</li>
<li>当一个作业终止时，调度程序会决定增加一个或多个新作业</li>
<li>若处理器的空闲时间片超过了某个阈值，可能会启动长程调度程序</li>
</ul>
</li>
<li>决定接受哪些作业，将其转化为进程
<ul>
<li>可以基于先来先服务（First-come-first-service，FCFS）的原则</li>
<li>可以基于系统性能管理工具，所用的原则包括：优先级（include priorty）、期望执行时间（expected execution time）、I/O请求（I/O requirements）</li>
<li>对于分时系统中交互的程序，操作系统接受所有的授权用户，直到系统饱和</li>
</ul>
</li>
</ul>
<h3 id="中程调度">中程调度</h3>
<p>中程调度是交换功能的一部分。（在之前的章节中分析过）</p>
<p>在典型情况下，换入（swapping-in）决定取决于管理系统并发度的需求。</p>
<h3 id="短程调度">短程调度</h3>
<p>短程调度程序也被称为<strong>分派器（dispatcher）</strong>。</p>
<blockquote>
<p>执行的频繁程度：</p>
<ul>
<li>长程调度程序执行的频率相对较低</li>
<li>中程调度程序执行得较为频繁</li>
<li>短程调度程序执行得最为频繁</li>
</ul>
</blockquote>
<p>导致当前进程阻塞或抢占当前运行进程的事件发生时，调用短程调度程序。这类事件包括：</p>
<ul>
<li>时钟中断（Clock interrupts）</li>
<li>I/O中断（I/O interrupts）</li>
<li>操作系统调用（Operating system calls）</li>
<li>信号（Signals）（如信号量）</li>
</ul>
<h2 id="调度算法">调度算法</h2>
<h3 id="短程调度准则">短程调度准则</h3>
<p>短程调度的主要目标是：按照优化系统一个或多个方面行为的方式，来分配处理器时间。</p>
<p>短程调度准则（Short-Tem Scheduling Criteria）从两个维度来划分：</p>
<ol>
<li>
<p>面向用户的规则（User-oriented）</p>
<ul>
<li>
<p>交互式系统中的响应时间</p>
<p>响应时间：指从提交一条请求到输出响应所经历的时间间隔。（对用户可见）</p>
</li>
</ul>
</li>
<li>
<p>面向系统的规则（System-oriented）</p>
<ul>
<li>
<p>处理器使用的效果和效率</p>
</li>
<li>
<p>吞吐量：即进程完成的速度</p>
<p>吞吐量是系统性能的一个重要测度，我们总是希望系统的吞吐量能达到最大。</p>
</li>
</ul>
</li>
</ol>
<p>从另一个维度去判别短程调度算法的优劣就是性能相关（Performance-related）：</p>
<ul>
<li>与性能相关的规则是定量的，例如：响应时间、吞吐率。</li>
<li>与性能无关的规则本质上要么是定性的，要么不易测量和分析，例如：可预测性。</li>
</ul>
<p>下面四张表总结了几种重要的调度规则：</p>
<ul>
<li>
<p>面向用户，与性能相关</p>
<table>
<thead>
<tr>
<th>调度规则</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>周转时间（Turnaround time）</td>
<td>指一个进程从提交到完成之间的时间间隔，包括实际执行时间和等待资源（包括处理器资源）的时间。对批处理作业而言，这是一种很合适的测度</td>
</tr>
<tr>
<td>响应时间（Response time）</td>
<td>对一个交互进程来说，这指从提交一个请求到开始接收响应之间的时间间隔。通常情况下，进程在处理该请求的同时，会开始给用户产生一些输出。因此从用户的角度来看，相对于周转时间，这是一种更好的测度。该调度原则会试图实现较低的响应时间，并在可接受的响应时间范围内，使可交互的用户数量最大</td>
</tr>
<tr>
<td>最后期限（Deadlines）</td>
<td>在能指定进程完成的最后期限时，调度原则将降低其他目标，使得满足最后期限的作业数量的百分比最大</td>
</tr>
</tbody>
</table>
</li>
<li>
<p>面向用户，其他</p>
<table>
<thead>
<tr>
<th>调度规则</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>可预测性（Predictability）</td>
<td>无论系统的负载如何，一个给定作业运行的总时间量和总代价是相同的。用户不希望响应时间或周转时间的变化太大。这可能需要在系统作业负载大范围抖动时发出信号，或需要系统处理不稳定性</td>
</tr>
</tbody>
</table>
</li>
<li>
<p>面向系统，与性能相关</p>
<table>
<thead>
<tr>
<th>调度规则</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>吞吐量（Throughput）</td>
<td>调度策略应使得单位时间内完成的进程数量最大。这是对能执行多少作业的一种度量。它明显取决于一个进程的平均执行长度，也受调度策略的影响，因为调度策略会影响利用率</td>
</tr>
<tr>
<td>处理器利用率（Processor utilization）</td>
<td>这是处理器处于忙状态的时间百分比。对昂贵的共享系统来说，这个规则很重要。在单用户系统和其他一些系统如实时系统中，该规则与其他规则相比不太重要</td>
</tr>
</tbody>
</table>
</li>
<li>
<p>面向系统。其他</p>
<table>
<thead>
<tr>
<th>调度规则</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>公平性（Fairness）</td>
<td>没有来自用户或其他系统的指导时，进程应被平等地对待，没有进程处于饥饿状态</td>
</tr>
<tr>
<td>强制优先级（Enforcing priorities）</td>
<td>指定进程的优先级后，调度策略应优先选择高优先级的进程</td>
</tr>
<tr>
<td>平衡资源（Balancing resources）</td>
<td>调度策略使系统中的所有资源处于忙状态，优先调度较少使用紧缺资源的进程。该规则也适用于中程调度和长程调度</td>
</tr>
</tbody>
</table>
</li>
</ul>
<p>设计调度策略时，要在互相竞争的各种要求之间进行折中。</p>
<blockquote>
<p>提供较好的响应时间可能需要调度算法在进程间频繁切换，而这会增加系统开销，降低吞吐量。</p>
</blockquote>
<h3 id="优先级的使用">优先级的使用</h3>
<p>在许多系统中，每个进程都被指定一个优先级，调度程序总是优先选择具有较高优先级的进程。（优先级从最高0开始）</p>
<p>下图展示了优先级排队的队列图（简化）：</p>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E4%BC%98%E5%85%88%E7%BA%A7%E7%9A%84%E4%BD%BF%E7%94%A8%E9%98%9F%E5%88%97.png" alt=""></p>
<ul>
<li>
<p>每个优先级有一个队列：RQ0 &gt; RQ1 &gt; … &gt; RQn</p>
<p>进行调度选择时总是从优先级最高的队列中检查，如果有进程，则对该进程进行调度。只有当较高优先级的队列都为空时，采取调度优先级较低的队列中的进程。</p>
</li>
<li>
<p>这种纯优先级调度的方案可能会引起较低优先级进程饥饿。</p>
<blockquote>
<p>如果不希望出现饥饿的状态，只能让一个进程的优先级随时间或执行历史而变化。</p>
</blockquote>
</li>
</ul>
<h3 id="选择调度策略">选择调度策略</h3>
<p>决策模式（Decision Mode ）</p>
<ul>
<li>
<p>非抢占（Nonpreemptive）：一旦进程处于运行状态，就会不断执行直到终止。进程要么因为等待I/O，要么因为请求某些操作系统服务而被阻塞。</p>
</li>
<li>
<p>抢占（Preemptive）：当前正运行进程可能被操作系统中断，并转换为就绪态。</p>
<blockquote>
<p>一个新进程到达时，或中断发生后把一个阻塞态进程置为就绪态时，或出现周期性的时间中断时，需要进行抢占决策。</p>
</blockquote>
</li>
</ul>
<blockquote>
<ul>
<li>非抢占模式会导致处理器效率降低</li>
<li>抢占模式可能违反公平</li>
</ul>
</blockquote>
<p>下面我们来分析六种进程调度算法，以下表中五个进程为例：</p>
<table>
<thead>
<tr>
<th>进程(Process）</th>
<th>到达时间（Arrival Time）</th>
<th>服务时间（Service Time）</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>B</td>
<td>2</td>
<td>6</td>
</tr>
<tr>
<td>C</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>D</td>
<td>6</td>
<td>5</td>
</tr>
<tr>
<td>E</td>
<td>8</td>
<td>2</td>
</tr>
</tbody>
</table>
<blockquote>
<p>注：这里的到达时间和服务时间都是以时间片的形式展示</p>
<ul>
<li>达到时间：进程进入队列的到达时刻</li>
<li>服务时间：进程需要多少时间片可以结束</li>
</ul>
</blockquote>
<p>以下具体描述进程的调度用**甘特图（Gantt chart）**的形式：</p>
<ul>
<li>非抢占：在甘特图中是连续的</li>
<li>抢占：在甘特图中是非连续的</li>
</ul>
<blockquote>
<p>注意，以下关于“当前队列”的描述：自左<code>（队首，进队）</code>向右<code>（队尾，出队）</code></p>
</blockquote>
<h4 id="先来先服务（FCFS）">先来先服务（FCFS）</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E5%85%88%E6%9D%A5%E5%85%88%E6%9C%8D%E5%8A%A1.png" alt=""></p>
<blockquote>
<p>说明：先来先服务（First-Come-First-Served，FCFS）</p>
<ul>
<li>0时刻，A进程进入队列，并开始执行，连续执行3个时间片</li>
<li>2时刻（此时A进程在执行），B进程进入队列，并等待执行</li>
<li>3时刻，A进程执行完毕，B进程开始执行，连续执行6个时间片</li>
<li>以此类推</li>
</ul>
</blockquote>
<ul>
<li>
<p>这是一种非抢占的算法</p>
</li>
<li>
<p>在当前进程执行完毕的时刻，调度器会选择队列中等待时间最长的进程，分配处理器资源</p>
</li>
<li>
<p>分析该种算法存在问题：对于短进程（服务时间短）和I/O密集型的进程不利。</p>
<blockquote>
<p>以银行取钱为例：</p>
<p>假设我要到银行取钱（办理取钱业务仅需5分钟），但是排在我前面的二十位客户都要开户（办理开户业务需要30分钟），那么我原本仅需5分钟的业务需要等待10h 5min。</p>
</blockquote>
<blockquote>
<p>相比于I/O密集型更有利于处理器密集型的进程：I/O进程必须等待CPU限制进程完成。</p>
<p>FCFS可能导致处理器和I/O设备都未得到充分利用。</p>
</blockquote>
</li>
</ul>
<h4 id="轮转（RR）">轮转（RR）</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E8%BD%AE%E8%BD%AC.png" alt=""></p>
<p>假设轮转的时钟周期 q = 1</p>
<blockquote>
<p>说明：轮转（Round-Robin，RR）</p>
<ul>
<li>0时刻，A进程进入队列，并开始执行，执行两个时间片</li>
<li>2-时刻，B进程进入队列，A进程在2时刻也重新进入队列（此时队列：A、B）</li>
<li>2+时刻，B进程开始执行</li>
<li>以此类推</li>
</ul>
</blockquote>
<ul>
<li>
<p>轮转是一种基于时钟的抢占算法</p>
</li>
<li>
<p>提出的目的在于解决FCFS短进程长时等待的问题，保证每个进程都占用处理器一段时间</p>
</li>
<li>
<p>周期性产生时钟中断，当中断发生时，当前运行的进程就被放置就绪队列中，进而调度下一个进程</p>
</li>
<li>
<p>关于时间片（as time slicing ）的大小设置：</p>
<ul>
<li>
<p>时间片过小：会产生无效的调度，即进程还没来得及执行就被调出了（高频次的进程调度也会带来巨大的开销）</p>
</li>
<li>
<p>时间片过大：退化回FCFS算法</p>
<blockquote>
<p>RR算法从根本上讲并没有解决I/O密集型等待CPU密集型进程的问题</p>
</blockquote>
</li>
<li>
<p>通常时间片的长度设置为：一次交互（产生输出）的时间</p>
</li>
</ul>
</li>
</ul>
<p>基于轮转法又提出了一种改进：虚拟轮转法（Virtual Round-Robin Scheduler，VRR）</p>
<p>引入一个辅助队列，解决了I/O进程长时等待的问题。</p>
<ul>
<li>一个进程因I/O而阻塞时，会加入一个I/O队列</li>
<li>解除了I/O阻塞的进程都会转移到一个FCFS辅助队列中。进行调度决策时，辅助队列中的进程优先于就绪队列中的进程。</li>
</ul>
<h4 id="最短进程优先（SPN）">最短进程优先（SPN）</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E6%9C%80%E7%9F%AD%E8%BF%9B%E7%A8%8B%E4%BC%98%E5%85%88.png" alt=""></p>
<blockquote>
<p>说明：最短进程优先（Shortest Process Next ，SPN）</p>
<ul>
<li>0时刻，A进程进入队列，并开始执行，连续执行3个时间片</li>
<li>2时刻（此时A进程在执行），B进程进入队列，并等待执行</li>
<li>3时刻，A进程执行完毕，判断队列中只有B进程，B进程开始执行，并连续执行6个时间片</li>
<li>4时刻（此时B进程正在执行），C进程进入队列，并等待执行（此时队列：C）</li>
<li>6时刻（此时B进程正在执行），D进程进入队列，并等待执行（此时队列：D、C）</li>
<li>8时刻（此时B进程正在执行），E进程进入队列，并等待执行（此时队列：E、D、C）</li>
<li>9时刻，B进程执行完毕，判断队列中服务时间最小的进程为E，所以执行E进程（此时队列：D、C）</li>
<li>以此类推</li>
</ul>
</blockquote>
<ul>
<li>
<p>这是一种非抢占的算法</p>
</li>
<li>
<p>原则：短进程优先（这也是该算法的初衷：为了解决FCFS短进程长时等待的问题）</p>
</li>
<li>
<p>潜在的问题：</p>
<ul>
<li>
<p>需要估计每个进程的服务时间（预测）。若估计值远低于实际运行时间，系统可能终止该作业。</p>
<blockquote>
<p>预估时间的算法：<strong>指数平均法（Exponential averaging）</strong>。一种基于过去时间值的时间序列预测将来值得算法。</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mi>S</mi><mrow><mi>n</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>=</mo><mi>α</mi><msub><mi>T</mi><mi>n</mi></msub><mo>+</mo><mtext>（</mtext><mn>1</mn><mo>−</mo><mi>α</mi><mtext>）</mtext><msub><mi>S</mi><mi>n</mi></msub></mrow><annotation encoding="application/x-tex">S_{n+1} = αT_n + （1-α）S_n
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.891661em;vertical-align:-0.208331em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.301108em;"><span style="top:-2.5500000000000003em;margin-left:-0.05764em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">+</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.208331em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.15em;"></span><span class="mord mathnormal" style="margin-right:0.0037em;">α</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:-0.13889em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.76666em;vertical-align:-0.08333em;"></span><span class="mord cjk_fallback">（</span><span class="mord">1</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span></span><span class="base"><span class="strut" style="height:0.83333em;vertical-align:-0.15em;"></span><span class="mord mathnormal" style="margin-right:0.0037em;">α</span><span class="mord cjk_fallback">）</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.151392em;"><span style="top:-2.5500000000000003em;margin-left:-0.05764em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>
<p>α是一个常数加权因子（0 &gt; α &gt; 1），用于确定距现在较近或较远的观测数据的相对权值。</p>
</blockquote>
</li>
<li>
<p>还可能导致长进程饥饿</p>
</li>
</ul>
</li>
</ul>
<h4 id="最短剩余时间（SRT）">最短剩余时间（SRT）</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E6%9C%80%E7%9F%AD%E5%89%A9%E4%BD%99%E6%97%B6%E9%97%B4.png" alt=""></p>
<blockquote>
<p>说明：最短剩余时间（Shortest Remaining Time ，SRT）</p>
<p>注意：当前队列中进程后面的数字表示剩余时间（还有多少时间片执行完毕）</p>
<ul>
<li>0时刻，A进程进入队列，并开始执行，连续执行2个时间片</li>
<li>2-时刻，B进程进入队列，2时刻A进程重新进入队列（此时队列：A(1)、B(6)）</li>
<li>2+时刻，执行A进程</li>
<li>3时刻，A进程执行完毕，此时队列中只有B进程，因此B进程执行</li>
<li>4-时刻，C进程进入队列，4时刻B进程重新进入队列（此时队列：B(5)、C(4)）</li>
<li>4+时刻，执行C进程，连续执行两个时间片</li>
<li>6-时刻，D进程进入队列，6时刻C进程重新进入队列（此时队列：C(2)、D(5)、B(5)）</li>
<li>6+时刻，执行C进程</li>
<li>以此类推</li>
</ul>
</blockquote>
<ul>
<li>SRT算法是最短进程优先（SPN）的抢占版本</li>
<li>潜在的问题：
<ul>
<li>同样需要估计每个进程的服务时间（预测），还需要记录过去的服务时间（增加开销）</li>
<li>仍然可能导致长进程饥饿</li>
</ul>
</li>
</ul>
<h4 id="最高响应比优先（HRRN）">最高响应比优先（HRRN）</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E6%9C%80%E9%AB%98%E5%93%8D%E5%BA%94%E6%AF%94%E4%BC%98%E5%85%88.png" alt=""></p>
<blockquote>
<p>说明：最高响应比优先（Highest Response Ratio Next，HRRN）</p>
<p>注意：当前队列中进程后面的数字表示响应比</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>R</mi><mo stretchy="false">(</mo><mtext>响应比</mtext><mo stretchy="false">)</mo><mo>=</mo><mfrac><mrow><mi>w</mi><mo>+</mo><mi>s</mi></mrow><mi>s</mi></mfrac></mrow><annotation encoding="application/x-tex">R(响应比) = \frac{w+s}{s}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mopen">(</span><span class="mord cjk_fallback">响应比</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:1.9463300000000001em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.2603300000000002em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord mathnormal">s</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222222222222222em;"></span><span class="mord mathnormal">s</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p>
<ul>
<li>w（time spent waiting）：等待处理器的时间。
<ul>
<li>w = 当前时间 - 到达时间(Arrival Time)</li>
</ul>
</li>
<li>s（expected service time）：预计的服务时间。
<ul>
<li>s = 服务时间(Service Time)</li>
</ul>
</li>
</ul>
<p>分析：</p>
<ul>
<li>0时刻，A进程进入队列，并开始执行，连续执行3个时间片</li>
<li>2时刻（此时A进程在执行），B进程进入队列，并等待执行（此时队列：B(1)）</li>
<li>3时刻，A进程执行完毕，队列中只有B进程，执行B进程</li>
<li>4时刻（此时B进程在执行），C进程进入队列，并等待执行（此时队列：C(1)）</li>
<li>6时刻（此时B进程在执行），D进程进入队列，并等待执行（此时队列：D(1)、C(3/2)）</li>
<li>8时刻（此时B进程在执行），E进程进入队列，并等待执行（此时队列：E(1)、D(7/5)、C(2)）</li>
<li>9时刻，B进程执行完毕，此时队列：E(3/2)、D(8/5)、C(9/4)，C进程的响应比最高，因此执行C进程</li>
<li>以此类推</li>
</ul>
</blockquote>
<ul>
<li>是一种非抢占的算法</li>
<li>优势：
<ul>
<li>如果等待时间相同，则短进程优先</li>
<li>长进程等待时间越久，其优先级会逐渐提升</li>
</ul>
</li>
<li>潜在的问题：依旧需要估计每个进程的服务时间（预测），还有其他的开销</li>
</ul>
<h4 id="反馈法（Feedback）">反馈法（Feedback）</h4>
<p>如果不存在各个进程的预计服务时间（事实上我们也希望如此）,则不能使用SPN、SRT、HRRN算法。于是又提出了反馈法：</p>
<img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS反馈法.png" style="zoom:80%;" />
<p>反馈法（Feedback）：同样是考虑短进程优先，对于长时间占有处理器使用权的进程会给予”惩罚“，使其降低优先级。</p>
<ul>
<li>有多个优先级的队列（RQ0、RQ1、…、RQn）</li>
<li>新进程加载进队列时总是先进入优先级最高的队列（RQ0）</li>
<li>有轮转机制：
<ul>
<li>定长时间片（每个队列的时钟周期：q = 固定值）</li>
<li>变长时间片（不同队列的时钟周期不同：q = 2<sup>i</sup> ，i为队列号（从0开始））</li>
</ul>
</li>
<li>当一个正在执行的进程被调出时，会调到比之前低一级的队列中</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E5%8F%8D%E9%A6%88%E6%B3%952.png" alt=""></p>
<blockquote>
<p>说明：假设为定长时间片：q = 1</p>
<ul>
<li>0时刻，A进程进入队列<code>RQ0</code>，执行A进程，持续执行2个时间片</li>
<li>2-时刻，B进程进入队列<code>RQ0</code>，2时刻A进程被调入到队列<code>RQ1</code>，此时
<ul>
<li>队列<code>RQ0</code>：B</li>
<li>队列<code>RQ1</code>：A</li>
</ul>
</li>
<li>2+时刻，执行B进程</li>
<li>3时刻，B进程被调入到队列<code>RQ1</code>，此时
<ul>
<li>队列<code>RQ0</code>：无进程</li>
<li>队列<code>RQ1</code>：B、A</li>
</ul>
</li>
<li>3+时刻，执行A进程</li>
<li>4-时刻，C进程进入队列<code>RQ0</code>，A进程执行完毕，此时
<ul>
<li>队列<code>RQ0</code>：C</li>
<li>队列<code>RQ1</code>：B</li>
</ul>
</li>
<li>4+时刻，执行C进程</li>
<li>5时刻，C进程被调入到队列<code>RQ1</code>，此时
<ul>
<li>队列<code>RQ0</code>：无进程</li>
<li>队列<code>RQ1</code>：C、B</li>
</ul>
</li>
<li>5+时刻，执行B进程</li>
<li>依次执行</li>
</ul>
</blockquote>
<h4 id="调度策略的比较">调度策略的比较</h4>
<p><img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OS%E8%B0%83%E5%BA%A6%E7%AD%96%E7%95%A5%E7%9A%84%E6%AF%94%E8%BE%83.png" alt=""></p>
<blockquote>
<p>说明：</p>
<ul>
<li>Finish Time（完成时间）：进程最终结束的时刻</li>
<li>Turnaround Time (T<sub>r</sub> ，周转时间)：完成时间 - 到达时间</li>
<li>T<sub>r</sub> / T<sub>s</sub>：周转时间 / 服务时间</li>
<li>最后一列：平均值</li>
</ul>
</blockquote>
<hr>
<h1>后记</h1>
<p>本篇已完结</p>
<p>（如有修改或补充欢迎评论）</p>
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/wechat.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/alipay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/03/14/操作系统/操作系统学习笔记-计算机系统概述/" title="操作系统学习笔记-计算机系统概述"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-14</div><div class="title">操作系统学习笔记-计算机系统概述</div></div></a></div><div><a href="/2021/03/28/操作系统/操作系统学习笔记/" title="操作系统学习笔记"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage7.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-28</div><div class="title">操作系统学习笔记</div></div></a></div><div><a href="/2021/06/02/操作系统/操作系统学习笔记-I-O管理和磁盘调度/" title="操作系统学习笔记-I/O管理和磁盘调度"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-02</div><div class="title">操作系统学习笔记-I/O管理和磁盘调度</div></div></a></div><div><a href="/2021/04/25/操作系统/操作系统学习笔记-信号量相关问题/" title="操作系统学习笔记-信号量相关问题"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-25</div><div class="title">操作系统学习笔记-信号量相关问题</div></div></a></div><div><a href="/2021/05/23/操作系统/操作系统学习笔记-内存管理/" title="操作系统学习笔记-内存管理"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-05-23</div><div class="title">操作系统学习笔记-内存管理</div></div></a></div><div><a href="/2021/05/04/操作系统/操作系统学习笔记-并发：死锁和饥饿/" title="操作系统学习笔记-并发：死锁和饥饿"><img class="cover" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/OperatingSystemscover1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-05-04</div><div class="title">操作系统学习笔记-并发：死锁和饥饿</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Twikoo</span><span class="switch-btn"></span><span class="second-comment">Valine</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">花猪</div><div class="author-info__description">此站点就暂时停留在秋天了吧</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">86</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/CNhuazhu"><i class="fab fa-github"></i><span>我的github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/CNhuazhu" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">花猪在此欢迎您光临小破站 (՞•Ꙫ•՞) <br><b>此站点已暂停更新！！！！</b><br><br>  <b><i>本站已部署至云服务器👇</i></b>：<a href="http://cnhuazhu.top">http://cnhuazhu.top</a> <br><br>  <b><i>github地址（暂停更新）</i></b>：<a target="_blank" rel="noopener external nofollow noreferrer" href="https://cnhuazhu.github.io/">https://cnhuazhu.github.io/</a> <br> <b><i>码云地址（暂停更新）</i></b>：<a target="_blank" rel="noopener external nofollow noreferrer" href="https://cnhuazhu.gitee.io">https://cnhuazhu.gitee.io</a> <br><br>  本站主题：Butterfly 3.7.8 <br> <img src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/pig.gif"></div><timing></timing></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">2.</span> <span class="toc-text">第九章：单处理器调度</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%84%E7%90%86%E5%99%A8%E8%B0%83%E5%BA%A6%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.1.</span> <span class="toc-text">处理器调度的类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%BF%E7%A8%8B%E8%B0%83%E5%BA%A6"><span class="toc-number">2.1.1.</span> <span class="toc-text">长程调度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%AD%E7%A8%8B%E8%B0%83%E5%BA%A6"><span class="toc-number">2.1.2.</span> <span class="toc-text">中程调度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9F%AD%E7%A8%8B%E8%B0%83%E5%BA%A6"><span class="toc-number">2.1.3.</span> <span class="toc-text">短程调度</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95"><span class="toc-number">2.2.</span> <span class="toc-text">调度算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9F%AD%E7%A8%8B%E8%B0%83%E5%BA%A6%E5%87%86%E5%88%99"><span class="toc-number">2.2.1.</span> <span class="toc-text">短程调度准则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BC%98%E5%85%88%E7%BA%A7%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.2.2.</span> <span class="toc-text">优先级的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E8%B0%83%E5%BA%A6%E7%AD%96%E7%95%A5"><span class="toc-number">2.2.3.</span> <span class="toc-text">选择调度策略</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%88%E6%9D%A5%E5%85%88%E6%9C%8D%E5%8A%A1%EF%BC%88FCFS%EF%BC%89"><span class="toc-number">2.2.3.1.</span> <span class="toc-text">先来先服务（FCFS）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BD%AE%E8%BD%AC%EF%BC%88RR%EF%BC%89"><span class="toc-number">2.2.3.2.</span> <span class="toc-text">轮转（RR）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E7%9F%AD%E8%BF%9B%E7%A8%8B%E4%BC%98%E5%85%88%EF%BC%88SPN%EF%BC%89"><span class="toc-number">2.2.3.3.</span> <span class="toc-text">最短进程优先（SPN）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E7%9F%AD%E5%89%A9%E4%BD%99%E6%97%B6%E9%97%B4%EF%BC%88SRT%EF%BC%89"><span class="toc-number">2.2.3.4.</span> <span class="toc-text">最短剩余时间（SRT）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%80%E9%AB%98%E5%93%8D%E5%BA%94%E6%AF%94%E4%BC%98%E5%85%88%EF%BC%88HRRN%EF%BC%89"><span class="toc-number">2.2.3.5.</span> <span class="toc-text">最高响应比优先（HRRN）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8D%E9%A6%88%E6%B3%95%EF%BC%88Feedback%EF%BC%89"><span class="toc-number">2.2.3.6.</span> <span class="toc-text">反馈法（Feedback）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B0%83%E5%BA%A6%E7%AD%96%E7%95%A5%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-number">2.2.3.7.</span> <span class="toc-text">调度策略的比较</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">3.</span> <span class="toc-text">后记</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/11/02/NodeJS/%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BA%E4%B8%80%E4%B8%AAexpress%E6%9C%8D%E5%8A%A1%E5%99%A8/" title="快速搭建一个express服务器"><img src="https://gitee.com/CNhuazhu/tu-chuang1/raw/master/img-blog/Node_Express_cover.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="快速搭建一个express服务器"/></a><div class="content"><a class="title" href="/2021/11/02/NodeJS/%E5%BF%AB%E9%80%9F%E6%90%AD%E5%BB%BA%E4%B8%80%E4%B8%AAexpress%E6%9C%8D%E5%8A%A1%E5%99%A8/" title="快速搭建一个express服务器">快速搭建一个express服务器</a><time datetime="2021-11-02T00:39:53.000Z" title="发表于 2021-11-02 08:39:53">2021-11-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/28/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Nginx-uWSGI-Flask%E9%83%A8%E7%BD%B2%E8%87%B3Ubuntu/" title="Nginx+uWSGI+Flask部署至Ubuntu"><img src="https://gitee.com/CNhuazhu/tu-chuang1/raw/master/img-blog/Ubuntu_Server_cover.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Nginx+uWSGI+Flask部署至Ubuntu"/></a><div class="content"><a class="title" href="/2021/10/28/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Nginx-uWSGI-Flask%E9%83%A8%E7%BD%B2%E8%87%B3Ubuntu/" title="Nginx+uWSGI+Flask部署至Ubuntu">Nginx+uWSGI+Flask部署至Ubuntu</a><time datetime="2021-10-28T03:01:09.000Z" title="发表于 2021-10-28 11:01:09">2021-10-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/18/Xftp%E4%BC%A0%E8%BE%93%E6%96%87%E4%BB%B6%E5%A4%B1%E8%B4%A5%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" title="Xftp传输文件失败解决方案"><img src="https://gitee.com/CNhuazhu/tu-chuang1/raw/master/img-blog/Xftp_cover.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Xftp传输文件失败解决方案"/></a><div class="content"><a class="title" href="/2021/10/18/Xftp%E4%BC%A0%E8%BE%93%E6%96%87%E4%BB%B6%E5%A4%B1%E8%B4%A5%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" title="Xftp传输文件失败解决方案">Xftp传输文件失败解决方案</a><time datetime="2021-10-18T09:50:16.000Z" title="发表于 2021-10-18 17:50:16">2021-10-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/13/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Hexo%E9%83%A8%E7%BD%B2%E8%87%B3%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88%E7%BB%AD%EF%BC%89%E2%80%94%E2%80%94%E5%BB%BA%E7%AB%8B%E5%BC%95%E5%AF%BC%E9%A1%B5%E5%8F%8A%E5%88%86%E7%AB%99/" title="Hexo部署至服务器（续）——建立引导页及分站"><img src="https://gitee.com/CNhuazhu/tu-chuang1/raw/master/img-blog/Ubuntu_Server_cover.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Hexo部署至服务器（续）——建立引导页及分站"/></a><div class="content"><a class="title" href="/2021/10/13/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Hexo%E9%83%A8%E7%BD%B2%E8%87%B3%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88%E7%BB%AD%EF%BC%89%E2%80%94%E2%80%94%E5%BB%BA%E7%AB%8B%E5%BC%95%E5%AF%BC%E9%A1%B5%E5%8F%8A%E5%88%86%E7%AB%99/" title="Hexo部署至服务器（续）——建立引导页及分站">Hexo部署至服务器（续）——建立引导页及分站</a><time datetime="2021-10-13T13:49:12.000Z" title="发表于 2021-10-13 21:49:12">2021-10-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/12/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Hexo%E9%83%A8%E7%BD%B2%E8%87%B3%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu-20-04%EF%BC%89/" title="Hexo部署至服务器（Ubuntu 20.04）"><img src="https://gitee.com/CNhuazhu/tu-chuang1/raw/master/img-blog/Ubuntu_Server_cover.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Hexo部署至服务器（Ubuntu 20.04）"/></a><div class="content"><a class="title" href="/2021/10/12/%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu%EF%BC%89/Hexo%E9%83%A8%E7%BD%B2%E8%87%B3%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%88Ubuntu-20-04%EF%BC%89/" title="Hexo部署至服务器（Ubuntu 20.04）">Hexo部署至服务器（Ubuntu 20.04）</a><time datetime="2021-10-12T07:13:40.000Z" title="发表于 2021-10-12 15:13:40">2021-10-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 By 花猪</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a target="_blank" rel="noopener external nofollow noreferrer" href="http://beian.miit.gov.cn/">蜀ICP备2021026748号</a><br>感谢光临<a href="http://cnhuazhu.top/" style="text-decoration:underline">小破站</a>,欢迎您提出宝贵的意见！</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script async="async">var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})
setTimeout(function(){preloader.endLoading();}, 3000);</script></div><div class="js-pjax"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css"><script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.css"><script>(() => {
  document.querySelectorAll('#article-container span.katex-display').forEach(item => {
    btf.wrap(item, 'div', '', 'katex-wrap')
  })
})()</script><script>(()=>{
  const $countDom = document.getElementById('twikoo-count')
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'hexo-1g41404w9f800e94',
      region: ''
    }, null))
  }

  const getCount = () => {
    twikoo.getCommentsCount({
      envId: 'hexo-1g41404w9f800e94',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      $countDom.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const loadTwikoo = (bool = false) => {
    if (typeof twikoo === 'object') {
      init()
      bool && $countDom && setTimeout(getCount,0)
    } else {
      getScript('https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js').then(()=> {
        init()
        bool && $countDom && setTimeout(getCount,0)
      })
    }
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo(true)
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'VHoX95KsBQSMyzEc3TdpmBkF-MdYXbMMI',
      appKey: 'qnwObDMbgMEKnu1Gp8tk7PYJ',
      placeholder: '欢迎留下你的评论（昵称和邮箱为必填项）',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: false,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Twikoo' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script src="/js/ip_content.js"></script><script src="/js/timing.js"></script><script src="https://cdn.jsdelivr.net/gh/xiabo2/CDN@latest/fishes.js"></script><script src="/js/footer.js"></script><script src="/js/ginkgo-leaf.js"></script><script id="canvas_nest" defer="defer" color="0,0,0" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div class="pjax-reload"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__fadeInLeft');
    arr[i].setAttribute('data-wow-duration', '600ms');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__fadeInRightBig');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer="defer" src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer="defer" src="/js/wow_init.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
                        function butterfly_swiper_injector_config(){
                          var parent_div_git = document.getElementById('recent-posts');
                          var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="2021/03/28/Hexo魔改/Hexo魔改记录/" alt=""><img width="48" height="48" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage4.jpg" alt=""/></a><div class="blog-slider__content"><span class="blog-slider__code">2021-03-28</span><a class="blog-slider__title" href="2021/03/28/Hexo魔改/Hexo魔改记录/" alt="">Hexo魔改记录</a><div class="blog-slider__text">Hexo魔改记录</div><a class="blog-slider__button" href="2021/03/28/Hexo魔改/Hexo魔改记录/" alt="">详情   </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="2021/08/02/Vue/Vue学习笔记/" alt=""><img width="48" height="48" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage5.jpg" alt=""/></a><div class="blog-slider__content"><span class="blog-slider__code">2021-08-02</span><a class="blog-slider__title" href="2021/08/02/Vue/Vue学习笔记/" alt="">Vue学习笔记</a><div class="blog-slider__text">本文将介绍Vue的基础语法</div><a class="blog-slider__button" href="2021/08/02/Vue/Vue学习笔记/" alt="">详情   </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="2021/03/28/Java/Java学习记录/" alt=""><img width="48" height="48" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage8.jpg" alt=""/></a><div class="blog-slider__content"><span class="blog-slider__code">2021-03-28</span><a class="blog-slider__title" href="2021/03/28/Java/Java学习记录/" alt="">Java学习记录</a><div class="blog-slider__text">Java学习记录</div><a class="blog-slider__button" href="2021/03/28/Java/Java学习记录/" alt="">详情   </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="2021/03/28/操作系统/操作系统学习笔记/" alt=""><img width="48" height="48" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage7.jpg" alt=""/></a><div class="blog-slider__content"><span class="blog-slider__code">2021-03-28</span><a class="blog-slider__title" href="2021/03/28/操作系统/操作系统学习笔记/" alt="">操作系统学习笔记</a><div class="blog-slider__text">记录操作系统学习历程</div><a class="blog-slider__button" href="2021/03/28/操作系统/操作系统学习笔记/" alt="">详情   </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="2021/03/28/计算机网络/计算机网络学习笔记/" alt=""><img width="48" height="48" src="https://cdn.jsdelivr.net/gh/CNhuazhu/Image/RotatingImage3.jpg" alt=""/></a><div class="blog-slider__content"><span class="blog-slider__code">2021-03-28</span><a class="blog-slider__title" href="2021/03/28/计算机网络/计算机网络学习笔记/" alt="">计算机网络学习笔记</a><div class="blog-slider__text">记录计算机网络学习历程</div><a class="blog-slider__button" href="2021/03/28/计算机网络/计算机网络学习笔记/" alt="">详情   </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
                          console.log('已挂载butterfly_swiper')
                          // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                          parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
                          }
                        if( document.getElementById('recent-posts') && (location.pathname ==='all'|| 'all' ==='all')){
                        butterfly_swiper_injector_config()
                        }
                      </script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://cdn.jsdelivr.net/npm/hexo-butterfly-swiper/lib/swiper_init.js"></script><!-- hexo injector body_end end --><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/assets/tororo.model.json"},"display":{"superSample":2,"width":200,"height":400,"position":"left","hOffset":0,"vOffset":-105},"mobile":{"show":false,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body></html>